package com.shop.item.api.service.impl;

import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.item.ItemCollectionInput;
import com.shop.common.model.param.item.ItemCommentInput;
import com.shop.common.model.param.item.ItemExtInput;
import com.shop.common.model.param.item.ItemFootInput;
import com.shop.common.model.result.item.ItemCollectionOutput;
import com.shop.common.model.result.item.ItemCommentOutput;
import com.shop.common.model.result.item.ItemExtOutput;
import com.shop.common.model.result.item.ItemFootOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.DateUtil;
import com.shop.common.util.state.DataStatus;
import com.shop.item.atom.atomsvc.IItemCollectionAtomService;
import com.shop.item.atom.atomsvc.IItemCommentAtomService;
import com.shop.item.atom.atomsvc.IItemExtAtomService;
import com.shop.item.api.service.IItemExtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemExtServiceImpl extends BaseService implements IItemExtService {
    @Autowired
    IItemCommentAtomService commentService;
    @Autowired
    IItemCollectionAtomService collectionService;
    @Autowired
    IItemExtAtomService extService;

    @Override
    public ServiceBean<ItemExtOutput> selectItemExt(Integer itemId) {
        ItemExtInput itemExtInput = new ItemExtInput();
        itemExtInput.setItemId(itemId);
        ServiceBean serviceBean = extService.selectItemExt(itemExtInput);
        return serviceBean;
    }

    @Override
    public ServiceBean<List<ItemFootOutput>> selectItemFootList4P(InputBean<ItemFootInput> inputBean) {
        return extService.selectItemFootList4P(inputBean);
    }

    @Override
    public ServiceBean deleteFoot(InputBean<Integer> inputBean) {
        ItemFootInput itemFootInput = new ItemFootInput();
        itemFootInput.setId(inputBean.getParam());
        return extService.deleteItemFoot(itemFootInput);
    }

    @Override
    public ServiceBean<List<ItemCollectionOutput>> selectItemCollectionList4P(InputBean<ItemCollectionInput> inputBean) {
        ServiceBean<List<ItemCollectionOutput>> serviceBean = collectionService.selectItemCollectionList4P(inputBean);
        return serviceBean;
    }

    @Override
    public ServiceBean<String> collectionItem(InputBean<ItemCollectionInput> inputBean) {

        ItemCollectionInput itemCollectionInput = inputBean.getParam();
        ServiceBean<ItemCollectionOutput> serviceBean = collectionService.selectItemCollection(inputBean.getParam());
        ItemExtInput itemExtInput = new ItemExtInput();
        itemExtInput.setItemId(itemCollectionInput.getItemId());
        ItemExtOutput itemExtOutput = extService.selectItemExt(itemExtInput).getData();
        /**
         * @Author zhoulk
         * @Description // 如果用户已经收藏该商品则取消收藏，反之亦然
         * @Date 17:36 2019/3/20
         *
         **/
        if (serviceBean.isSuccess()) {
            ItemCollectionOutput collectionOutput = serviceBean.getData();
            itemCollectionInput = new ItemCollectionInput();
            itemCollectionInput.setId(collectionOutput.getId());
            itemCollectionInput.setDataStatus(DataStatus.DELETE.value());
            collectionService.updateItemCollection(itemCollectionInput);
            itemExtInput.setCollection(itemExtOutput.getCollection() - 1);
        } else {
            itemCollectionInput.setCreateTime(DateUtil.newNowDate());
            itemCollectionInput.setDataStatus(DataStatus.USE.value());
            collectionService.insertItemCollection(itemCollectionInput);
            itemExtInput.setCollection(itemExtOutput.getCollection() + 1);
        }
        itemExtInput.setId(itemExtOutput.getId());
        extService.updateItemExt(itemExtInput);

        return resultSuccess();
    }

    @Override
    public ServiceBean cancelCollection(InputBean<ItemCollectionInput> inputBean) {
        return collectionService.deleteItemCollection(inputBean.getParam());
    }

    @Override
    public ServiceBean isCollection(InputBean<ItemCollectionInput> inputBean) {
        return collectionService.selectItemCollection(inputBean.getParam());
    }

    @Override
    public ServiceBean<List<ItemCommentOutput>> selectItemCommentList4P(InputBean<Integer> inputBean) {
        ItemCommentInput itemCommentInput = new ItemCommentInput();
        itemCommentInput.setItemId(inputBean.getParam());
        InputBean inputBean1 = new InputBean(itemCommentInput);
        inputBean1.setIndex(inputBean.getIndex());
        inputBean1.setPageSize(inputBean.getPageSize());
        return commentService.selectItemCommentList4P(inputBean1);
    }

    @Override
    public ServiceBean<String> insertItemComment(InputBean<ItemCommentInput> inputBean) {
        ItemCommentInput itemCommentInput = inputBean.getParam();
        itemCommentInput.setCreateTime(DateUtil.newNowDate());
        itemCommentInput.setDataStatus(DataStatus.USE.value());
        ServiceBean serviceBean = commentService.insertItemComment(itemCommentInput);
        if (!serviceBean.isSuccess()) {
            return serviceBean;
        }
        ItemExtInput itemExtInput = new ItemExtInput();
        itemExtInput.setItemId(itemCommentInput.getItemId());
        ItemExtOutput itemExtOutput = extService.selectItemExt(itemExtInput).getData();
        itemExtInput.setId(itemExtOutput.getId());
        itemExtInput.setComment(itemExtOutput.getComment() + 1);
        extService.updateItemExt(itemExtInput);
        return serviceBean;
    }

}
